package com.efounder.frame.baseui;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.efounder.http.EFHttpRequest;
import com.efounder.view.CommonQMEmptyView;
import com.efounder.view.GlobalLoadingDialog;
import com.efounder.view.IEmptyView;
import com.qmuiteam.qmui.widget.QMUIEmptyView;


public abstract class BaseFragment extends EFFragment {
    protected static String TAG = "";
    /**
     * Fragment当前状态是否可见
     */
    protected boolean isVisible;
    private boolean isAlone = true;

    /**
     * 是否是单独的fragment
     *
     * @return true 表示是单独的fragment ，我们可能会使用fragment自己的标题栏
     * false 表示不是单独的fragment，中多个fragment同时被初始化出来的情况，我们可能不使用fragment自己的标题栏
     */
    public boolean isAlone() {
        return isAlone;
    }

    public void setAlone(boolean isAlone) {
        this.isAlone = isAlone;
    }

    protected boolean istitleVisible = true;
    /*view是否已经创建*/
    protected boolean isViewCreated = false;

    boolean isShowBottom = true;

    /*右上角button设置*/
    private String rightButtonType;

    private boolean LinkedtoActivity = false;
    private String leftButtonType;
    private String rightTopButtonICon;

    public String getBadgeType() {
        return badgeType;
    }

    public void setBadgeType(String badgeType) {
        this.badgeType = badgeType;
    }

    /*
     * 角标归属类型 与主界面里的底部导航栏对应
     */
    private String badgeType = "";


    public boolean isLinkedtoActivity() {
        return LinkedtoActivity;
    }

    public void setLinkedtoActivity(boolean linkedtoActivity) {
        LinkedtoActivity = linkedtoActivity;
    }

    public String getRightButtonType() {
        return rightButtonType;
    }

    public void setRightButtonType(String rightButtonType) {
        this.rightButtonType = rightButtonType;
    }

    public String getLeftButtonType() {
        return leftButtonType;
    }

    public void setLeftButtonType(String leftButtonType) {
        this.leftButtonType = leftButtonType;
    }

    public String getRightTopButtonICon() {
        return rightTopButtonICon;
    }

    public BaseFragment setRightTopButtonICon(String rightTopButtonICon) {
        this.rightTopButtonICon = rightTopButtonICon;
        return this;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = getClass().getSimpleName();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (isViewCreated) {
            if (isVisibleToUser) {
                onVisible();
            } else {
                onInvisible();
            }
        }

    }

    /**
     * onCreateView 最后调用
     */
    protected void onCreateViewAfter_enable_onVisible_onInvisible() {
        // 
        if (isVisible || isAlone) {
            onVisible();
        } else {
            onInvisible();
        }

        isViewCreated = true;

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        onCreateViewAfter_enable_onVisible_onInvisible();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


    }

    /**
     * 可见
     */
    public void onVisible() {
    }


    /**
     * 不可见
     */
    public void onInvisible() {
    }

    @Override
    public void onDestroy() {
        isViewCreated = false;
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isAlone()) {
		/*if (isShowBottom)
			menuFrag.getBottomBar().setVisibility(BaseView.GONE);
			*/
        }
    }

    @Override
    public void onPause() {
        super.onPause();
		/*if (isShowBottom) {
			TabBottomActivity menuFrag = TabBottomActivity.getInstance();
			menuFrag.getBottomBar().setVisibility(BaseView.VISIBLE);
		}*/
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //要结束界面的时候取消网络请求
        EFHttpRequest.cancelRequest(TAG);
        if (globalLoadingDialog != null) {
            globalLoadingDialog.dismissLoading();
        }
        globalLoadingDialog = null;
    }

    //没有数据时显示的view
    private IEmptyView iEmptyView;
    private View contentListView;

    /**
     * 绑定空 view和显示数据的view
     *
     * @param qmuiEmptyView
     * @param view
     */
    @Deprecated
    public void bindEmptyView(QMUIEmptyView qmuiEmptyView, View view) {
        CommonQMEmptyView commonQMEmptyView = new CommonQMEmptyView();
        commonQMEmptyView.setQMUIEmptyView(qmuiEmptyView);
        this.iEmptyView = commonQMEmptyView;
        this.contentListView = view;
    }

    public void bindEmptyView(IEmptyView iEmptyView, View view) {
        this.iEmptyView = iEmptyView;
        this.contentListView = view;
    }

    /**
     * 用于显示纯文本的简单调用方法，此时loading、button均被隐藏
     *
     * @param titleText  标题的文字，不需要则传null
     * @param detailText 详情文字，不需要则传null
     */
    public void showEmptyView(String titleText, String detailText) {
        iEmptyView.show(titleText, detailText);

        contentListView.setVisibility(View.GONE);

    }

    /**
     * 用于显示纯文本的简单调用方法，此时loading、button均被隐藏
     *
     * @param titleText 标题的文字，不需要则传null
     */
    public void showEmptyView(String titleText, String buttonText, View.OnClickListener onClickListener) {
        iEmptyView.show(false, titleText, null, buttonText, onClickListener);
        contentListView.setVisibility(View.GONE);
    }

    /**
     * 显示网络异常
     */
    public void showNetError() {
        iEmptyView.showNetError();
        contentListView.setVisibility(View.GONE);
    }

    /**
     * 显示暂无数据
     */
    public void showNoDatas() {
        iEmptyView.showNoData();
        contentListView.setVisibility(View.GONE);
    }

    public void hideEmptyView() {
        iEmptyView.hide();
        contentListView.setVisibility(View.VISIBLE);
    }


    private GlobalLoadingDialog globalLoadingDialog;

    public void showLoading(String text) {

        if (globalLoadingDialog == null) {
            globalLoadingDialog = new GlobalLoadingDialog(getActivity());
        }
        globalLoadingDialog.showLoading(text);
    }

    public void dismissLoading() {
        if (globalLoadingDialog != null) {
            globalLoadingDialog.dismissLoading();
        }
    }

}